//
//  RHPowerStatusView.m
//  RuiHeAppApp
//
//  Created by bill on 2023/7/30.
//
#define  bottom_labHeigh 30
#import "RHPowerStatusView.h"
#import "RHPowerCircleView.h"
@interface RHPowerStatusView()
@property (nonatomic, strong) RHPowerCircleView *powerCircleView;

@property (nonatomic ,strong) QMUILabel *socLab;

@property (nonatomic ,strong) QMUILabel *sohLab;
@end
@implementation RHPowerStatusView
//-(instancetype)init
//{
//    if([super init]){
//        [self config];
//        [self createSubLayer];
//    }
//    return self;
//}
-(instancetype)initWithFrame:(CGRect)frame
{
    if([super initWithFrame:frame]){
        [self setupUI];
    
    }
    return self;
}
-(void)setImgSize:(CGFloat)imgSize
{
    _imgSize = imgSize;
    self.powerCircleView.imgSize = imgSize;
}

-(void)hidePowerAndVoltage
{
    UILabel *powerLab = [self viewWithTag:102];
    UILabel *voltageLab =  [self viewWithTag:103];
    powerLab.hidden = YES;
    voltageLab.hidden = YES;
}

-(void)setTopoModel:(RHTopoModel *)topoModel
{
    _topoModel = topoModel;
    self.powerCircleView.topoModel = topoModel;
    UILabel *socLab =   [self viewWithTag:100];
    UILabel *sohLab =   [self viewWithTag:101];
    UILabel *powerLab = [self viewWithTag:102];
    UILabel *voltageLab =  [self viewWithTag:103];
    NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
    paragraphStyle.lineSpacing = 5;
    paragraphStyle.alignment = NSTextAlignmentCenter;
    
    NSDictionary *attiDic = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:20],NSForegroundColorAttributeName:kCOLOR_HEX(@"3F4443")};
    
    NSString *soc = [NSString stringWithFormat:@"SOC  %@%%",ValidNullDataToString(topoModel.E_soc, @"0")];
    NSMutableAttributedString *attri_soc = [[NSMutableAttributedString alloc] initWithString:soc];
    [attri_soc setAttributes:attiDic range:[soc rangeOfString:[NSString stringWithFormat:@"%@%%",ValidNullDataToString(topoModel.E_soc, @"0")]]];
    
    
    NSString *soh = [NSString stringWithFormat:@"SOH  %@%%",ValidNullDataToString(topoModel.E_soh, @"0")];
    NSMutableAttributedString *attri_soh = [[NSMutableAttributedString alloc] initWithString:soh];
    [attri_soh setAttributes:attiDic range:[soh rangeOfString:[NSString stringWithFormat:@"%@%%",ValidNullDataToString(topoModel.E_soh, @"0")]]];
    
    NSString *power = [NSString stringWithFormat:@"Power  %.2fKW",[topoModel.E_tv floatValue]*[topoModel.E_cur floatValue]];
    NSMutableAttributedString *attri_power = [[NSMutableAttributedString alloc] initWithString:power];
    [attri_power setAttributes:attiDic range:[power rangeOfString:[NSString stringWithFormat:@"%.2fKW",[topoModel.E_tv floatValue]*[topoModel.E_cur floatValue]]]];
    
    NSString *volatage = [NSString stringWithFormat:@"Voltage  %.2fV",[topoModel.E_tv floatValue]];
    NSMutableAttributedString *attri_voltage = [[NSMutableAttributedString alloc] initWithString:volatage];
    [attri_voltage setAttributes:attiDic range:[volatage rangeOfString:[NSString stringWithFormat:@"%.2fV",[topoModel.E_tv floatValue]]]];
    
    socLab.attributedText     = attri_soc;
    sohLab.attributedText     = attri_soh;
    powerLab.attributedText   = attri_power;
    voltageLab.attributedText = attri_voltage;
}

-(void)setupUI
{
    _powerCircleView = [[RHPowerCircleView alloc]init];
    [self addSubview:_powerCircleView];
    kWeakSelf;
    [_powerCircleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(kScaleW(180));
        make.top.mas_offset(0);
        make.centerX.mas_offset(0);
    }];
    
    UIView *bottomView = kView;
    [self addSubview:bottomView];
    [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_offset(0);
        make.top.equalTo(weakSelf.powerCircleView.mas_bottom).offset(0);
    }];
    
    NSArray *titles =@[@"SoC:",@"SoH:",@"Power:",@"Voltage:"];
    __block UILabel *lastLabel;
    [titles enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        QMUILabel *titleLab = [[QMUILabel alloc] init];
        titleLab.tag = idx+100;
        [titleLab setTextAlignment:NSTextAlignmentCenter];
        [titleLab setText:obj];
        [titleLab setTextColor:idx == 0? FontColor(@"#56C271"):FontColor(@"#999999")];
        [titleLab setFont:[UIFont systemFontOfSize:15]];
        [bottomView addSubview:titleLab];
        
        [titleLab setText:[NSString stringWithFormat:@"%@ %@",titles[idx],@"--"]];
        
        if (idx == 0) {
            [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.top.mas_offset(0);
                make.width.equalTo(bottomView).multipliedBy(0.5);
                make.height.mas_offset(35);
            }];
        }else{
            //换行
            if(idx%2==0){
                [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.left.mas_offset(0);
                    make.top.equalTo(lastLabel.mas_bottom);
                    make.height.width.equalTo(lastLabel);
                }];
            }else{
                [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.left.equalTo(lastLabel.mas_right);
                    make.top.height.width.equalTo(lastLabel);
                }];
            }
            
        }
        //最后一个,封底
        if(idx == titles.count-1){
            [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
                make.bottom.mas_offset(0);
            }];
        }
        lastLabel = titleLab;
    }];
  
}





@end
